package com.example.codicefiscale;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

	protected int mYear;
	protected int mMonth;
	protected int mDay;
	private Function function;

	//protected TextView mDateDisplay;






	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);


		final EditText name=(EditText)findViewById(R.id.nome);
		final EditText surname=(EditText)findViewById(R.id.cognome);
		final TextView cf=(TextView)findViewById(R.id.cf);
		final RadioGroup rg=(RadioGroup)findViewById(R.id.sesso);
		int sex=rg.getCheckedRadioButtonId();


		function=new Function(mYear,mMonth,mDay,sex);


		Button b=(Button)findViewById(R.id.ok);
		Button date=(Button)findViewById(R.id.pickDate);

		b.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				cf.setText(Function.cognome(surname.getText().toString())+Function.nome(name.getText().toString()));

			}
		});

		date.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(0);
			}
		});
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		function.setmDay(mDay);
		function.setmMonth(mMonth);
		function.setmYear(mYear);

		cf.setText(cf.getText()+function.updateDisplay());

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}



	protected DatePickerDialog.OnDateSetListener mDateSetListener =
			new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, 
				int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;


			function.setmDay(mDay);
			function.setmMonth(mMonth);
			function.setmYear(mYear);
			
			//final TextView cf=(TextView)findViewById(R.id.cf);
			//cf.setText(cf.getText()+function.updateDisplay());
		}
	};

	protected Dialog onCreateDialog(int id) {
		return new DatePickerDialog(this,
				mDateSetListener,
				mYear, mMonth, mDay);
	}



}
